package com.ksd.modules.course.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.annotation.ApiLog;
import com.ksd.common.controller.BaseController;
import com.ksd.common.core.result.R;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.annotation.RepeatSubmit;
import com.ksd.modules.course.po.AdminCourseSavePO;
import com.ksd.modules.course.po.AdminCourseSearchPO;
import com.ksd.modules.course.service.IAdminCourseService;
import com.ksd.modules.course.vo.AdminCourseVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
public class AdminCourseController extends BaseController {
    
    @Autowired
    private IAdminCourseService adminCourseService;

    /**
     * 【课程】- 列表
     * @return
     */
    @PostMapping("/course/list")
    public R<Page<AdminCourseVo>> findAdminCourses(@Validated @RequestBody AdminCourseSearchPO adminCourseSearchPO){
        return R.data(adminCourseService.findAdminCourses(adminCourseSearchPO));
    }

    /**
     * 【课程】- 回收站
     * @return
     */
    @PostMapping("/course/recyle")
    public R<Page<AdminCourseVo>> findAdminCoursesRecyle(@Validated @RequestBody AdminCourseSearchPO adminCourseSearchPO){
        return R.data(adminCourseService.findAdminCoursesRecyle(adminCourseSearchPO));
    }

    /**
     * 【课程】 - 保存和更新
     * @param adminCourseSavePO
     * @return
     */
    @PostMapping("/course/saveupdate")
    @ApiLog("【课程】 - 保存和更新")
    @RepeatSubmit
    public R<AdminCourseVo> saveUpdateAdminCourse(@Validated @RequestBody AdminCourseSavePO adminCourseSavePO){
        return R.data(adminCourseService.saveUpdateAdminCourse(adminCourseSavePO));
    }


    /**
     * 【课程】 - 根据id查询明细
     * @param id
     * @return
     */
    @PostMapping("/course/get/{id}")
    public R<AdminCourseVo> getAdminCourseById(@PathVariable("id")Long id){
        return R.data(adminCourseService.getAdminCourseById(id));
    }

    /**
     * 【课程】 - 根据id删除课程
     * @param id
     * @return
     */
    @PostMapping("/course/del/{id}")
    @ApiLog("【课程】 - 根据id删除课程 ")
    public R<Boolean> deleteAdminCourseById(@PathVariable("id")Long id){
        return R.data(adminCourseService.deleteAdminCourseById(id));
    }

    /**
     * 【课程】- 根据id恢复删除
     *
     * @param id
     * @return
     */
    @PostMapping("/course/recover/{id}")
    @ApiLog("【课程】- 根据id恢复删除")
    @RepeatSubmit
    public R<Boolean> recoverByIdAdminCourse(@PathVariable("id") Long id) {
        return R.data(adminCourseService.recoverByIdAdminCourse(id));
    }

    /**
     * 【课程】- 批量逻辑删除
     *
     * @return
     */
    @PostMapping("/course/batch/dels")
    @ApiLog("【课程】- 批量逻辑删除")
    @RepeatSubmit
    public R<Boolean> bacthDeletesAdminCourse(@RequestBody BatchIdsPO batchIdsPO) {
        return R.data(adminCourseService.bacthDeletesAdminCourse(batchIdsPO));
    }


    /**
     * 【课程】- 批量恢复
     *
     * @return
     */
    @PostMapping("/course/batch/recover")
    @ApiLog("【课程】- 批量恢复")
    @RepeatSubmit
    public R<Boolean> batchRecoversAdminCourse(@RequestBody BatchIdsPO batchIdsPO) {
        return R.data(adminCourseService.batchRecoversAdminCourse(batchIdsPO));
    }
    
    /**
     * 【课程】- 批量物理删除
     *
     * @return
     */
    @PostMapping("/course/batch/remove")
    @ApiLog("【课程】- 批量物理删除")
    @RepeatSubmit
    public R<Boolean> batchRemovesAdminCourse(@RequestBody BatchIdsPO batchIdsPO) {
        return R.data(adminCourseService.batchRemovesAdminCourse(batchIdsPO));
    }

    /**
     * 【课程】- 根据ID恢复删除
     * @param id
     * @return
     */
    @PostMapping("/course/remove/{id}")
    @ApiLog("【课程】- 根据ID恢复删除")
    public R<Boolean> removeAdminCourseById(@PathVariable("id")Long id){
        return R.data(adminCourseService.removeAdminCourseById(id));
    }

    /**
     * 【课程】- 修改状态
     *
     * @return
     */
    @PostMapping("/course/status/{id}/{field}")
    @ApiLog("【课程】- 修改状态")
    @RepeatSubmit
    public R<Boolean> updateStatus(
            @PathVariable("id") Long id,
            @PathVariable("field") String field,
            @RequestParam("value") String value
    ) {
        if(StringUtils.isNumeric(value)) {
            return R.data(adminCourseService.updateStatus(id, field, Integer.parseInt(value)));
        }else{
            return R.data(adminCourseService.updateStatusStr(id, field, String.valueOf(value)));
        }
    }

    /**
     * 【课程】-批量上架
     *
     * @return
     */
    @PostMapping("/course/batch/up")
    @ApiLog("【课程】-批量上架")
    @RepeatSubmit
    public R<Boolean> batchUps(@RequestBody BatchIdsPO batchIdsPO) {
        return R.data(adminCourseService.batchUps(batchIdsPO));
    }

    /**
     * 【课程】-批量下架
     *
     * @return
     */
    @PostMapping("/course/batch/down")
    @ApiLog("【课程】-批量下架")
    @RepeatSubmit
    public R<Boolean> batchDowns(@RequestBody BatchIdsPO batchIdsPO) {
        return R.data(adminCourseService.batchDowns(batchIdsPO));
    }

    /**
     * 【课程】- 复制数据
     * @param opid
     * @return
     */
    @PostMapping("/course/copy/{opid}")
    @ApiLog("【课程】- 复制数据")
    @RepeatSubmit
    public R<Boolean> copyData(@PathVariable("opid")Long opid){
        return R.data(adminCourseService.copyData(opid));
    }
}
